/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package clip.model;

import L.L;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
import java.util.TimerTask;
import javax.swing.ListModel;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;

/**
 *
 * @author admin
 */
public class BuddyList extends Observable implements Observer, ListModel, Serializable {
    
    //wird alles mitgespeichert hier
    private List<Buddy> buddies = new ArrayList<Buddy>(5);
    
    
    //transient felder
    private transient List<ListDataListener> listDataListeners = new ArrayList<ListDataListener>(2);
    
    public BuddyList() {
        //TODO updateTimer
    }

    public class UpdateTimer extends TimerTask {
        @Override
        public void run() {
            for (Buddy b : buddies) {
                b.checkConnectionAlive();
            }
        }
    }
    
    public void tryToConnectToAllUnconnected() {
        for (Buddy b : buddies) {
            if (b.getStatus().equals(Buddy.CONNECTIONSTATUS.DISCONNECTED)) {
                b.connectToBuddy();
            }
        }
    }
    
    public Buddy getBuddyByNickname(String nickname) {
        for (Buddy b : buddies) {
            if (b.getNickname().equals(nickname)) {
                return b;
            }
        }
        return null;
    }

    public void connectAll() {
        for (Buddy b : buddies) {
            b.connectToBuddy();
        }
    }
    /////////////////////////////////////////////////
    //delegates für liste
    public Buddy get(int index) {
        return buddies.get(index);
    }

    public int size() {
        return buddies.size();
    }

    public boolean isEmpty() {
        return buddies.isEmpty();
    }

    public void clear() {
        buddies.clear();
    }

    public boolean add(Buddy b) {
        b.addObserver(this);
        boolean ret = buddies.add(b);
        this.notifyListDataChanged();
        return ret;
    }

    public boolean remove(Buddy b) {
        boolean ret = buddies.remove(b);
        this.notifyListDataChanged();
        return ret;
    }

    public boolean remove(int index) {
        return buddies.remove(buddies.get(index));
    }
    //LISTMODEL
    public int getSize() {
        return buddies.size();
    }

    public Object getElementAt(int index) {
        if (buddies.size() == 0) {
            return null;
        }
        return buddies.get(index);
    }
    

    public void notifyListDataChanged() {
        //this.setChangedAndNotify(null);
        for (ListDataListener l : listDataListeners) {
            l.contentsChanged(new ListDataEvent(this, ListDataEvent.CONTENTS_CHANGED, 0, buddies.size()));
        }
    }

    public void addListDataListener(ListDataListener l) {
        if (listDataListeners==null) listDataListeners=new ArrayList<ListDataListener>();
        listDataListeners.add(l);
        L.dd("listdatalistener geadded, " + l.toString());
    }

    public void removeListDataListener(ListDataListener l) {
        listDataListeners.remove(l);
    }

    public void update(Observable o, Object arg) {
        if (o instanceof Buddy) {
        }
        notifyListDataChanged();
    }

    @Override
    public String toString() {
        return buddies.toString();
    }
    
    
}
